package juctest;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: LockTest
 * @description: 等待唤醒
 * @date 2021/3/1917:04
 * @since V1.0
 */
public class WaitAndNotifyTest{
    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        Productor productor = new Productor(clerk);
        Consumer consumer = new Consumer(clerk);
        new Thread(productor, "生产者A").start();
        new Thread(consumer, "消费者B").start();
        new Thread(productor, "生产者C").start();
        new Thread(consumer, "消费者D").start();
    }
}

//店员
class Clerk {
    private int product = 0;//共享数据

    /**
     * synchronized修饰方法就相当于给方法中的代码块加this锁。
     * 但如果你释放锁了。方法就不会被锁住了
     * 如果方法中没有释放锁的操作，那这个方法是必须执行完的
     */
    public synchronized void get() { //进货
        while (product >= 1) {
            System.out.println("产品已满");
            try {
                this.wait();//满了就等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + ":" + (++product));
        this.notifyAll();//没满就可以进货
    }

    public synchronized void sell() {//卖货
        while (product <= 0) {
            System.out.println("缺货");
            try {
                this.wait();//缺货就等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + ":" + (--product));
        this.notifyAll();//不缺货就可以卖
    }
}

//生产者
class Productor implements Runnable {
    private Clerk clerk;

    public Productor(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        for (int i = 0; i < 21; i++) {
            clerk.get();
        }
    }
}

//消费者
class Consumer implements Runnable {
    private Clerk clerk;

    public Consumer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(300);//睡0.2秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 21; i++) {
            clerk.sell();
        }
    }
}